package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.domain.entity.PlatformMenu;
import com.gpframework.common.core.domain.entity.SysMenuVo;
import com.gpframework.common.core.domain.entity.SysUser;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.ServletUtils;
import com.gpframework.common.utils.ThreeDES2Utils;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.core.LoginUser;
import com.gpframework.module.module.domain.BrandInfo;
import com.gpframework.module.module.domain.req.BrandInfoAdd;
import com.gpframework.module.module.domain.req.BrandInfoReq;
import com.gpframework.module.module.domain.req.BrandInfoUpdate;
import com.gpframework.module.module.domain.vo.BrandInfoVo;
import com.gpframework.module.module.service.IBrandInfoService;
import com.gpframework.module.pay.utils.joinpay.sdk.utitls.StringUtil;
import com.gpframework.module.service.TokenService;
import com.gpframework.module.service.system.IPlatformMenuService;
import com.gpframework.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BrandInfoController
 * @Author gpframework
 * @Date 2022-03-23
 * @Version 1.0.0
 * @Description 品牌信息Controller
 */
@Validated
@Api(tags = "品牌信息")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/module/brandInfo")
public class BrandInfoController extends BaseController {

    /**
     * 品牌信息Service接口
     */
    private final IBrandInfoService iBrandInfoService;

    private final TokenService tokenService;

    private final IPlatformMenuService menuService;



    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:list')")
    @GetMapping("/list")

    public TableDataInfo<BrandInfoVo> list(BrandInfoReq brandInfoReq) {
        startPage();
        QueryWrapper<BrandInfo> lqw = new QueryWrapper<BrandInfo>();
        brandInfoReq.generatorQuery(lqw, true);
        List<BrandInfoVo> list = iBrandInfoService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:add')")
    @Log(title = "添加品牌信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody BrandInfoAdd brandInfoAdd) {
        BrandInfo brandInfo = Convert.convert(new TypeReference<BrandInfo>() {
        }, brandInfoAdd);
        return toAjax(iBrandInfoService.add(brandInfo) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:edit')")
    @Log(title = "修改品牌信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody BrandInfoUpdate brandInfoUpdate) {
        if (!StringUtils.isEmpty(brandInfoUpdate.getPassword())) {
            brandInfoUpdate.setShowPassword(ThreeDES2Utils.encode(brandInfoUpdate.getPassword()));
            brandInfoUpdate.setPassword(SecurityUtils.encryptPassword(brandInfoUpdate.getPassword()));
        }
        return toAjax(iBrandInfoService.edit(brandInfoUpdate) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:brandInfo:export')")
    @Log(title = "导出品牌信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BrandInfoReq brandInfoReq) {
        QueryWrapper<BrandInfo> lqw = new QueryWrapper<BrandInfo>();
        brandInfoReq.generatorQuery(lqw, true);
        List<BrandInfoVo> list = iBrandInfoService.listVo(lqw);
        ExcelUtil<BrandInfoVo> util = new ExcelUtil<BrandInfoVo>(BrandInfoVo.class);
        return util.exportExcel(list, "brandInfo");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:query')")
    @GetMapping(value = "/{brandId}")
    public AjaxResult<BrandInfoVo> getInfo(@PathVariable("brandId") Long brandId) {
        return AjaxResult.success(iBrandInfoService.getBrandInfo(brandId));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2022-03-23
     * @Param brandInfo
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:remove')")
    @Log(title = "删除品牌信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{brandIds}")
    public AjaxResult remove(@PathVariable Long[] brandIds) {
        return toAjax(iBrandInfoService.removeByIds(Arrays.asList(brandIds)) ? 1 : 0);
    }

    /**
     * 加载对应角色菜单列表树
     *
     * @param brandId
     * @return
     */
    @ApiOperation("加载对应角色菜单列表树")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:query')")
    @GetMapping(value = "/roleMenuTreeSelect/{brandId}")
    public Map roleMenuTreeSelect(@PathVariable Long brandId) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        PlatformMenu menu = new PlatformMenu();
        menu.setIsAuthorization(YesNoEnum.YES.getKey());
        List<PlatformMenu> menus = menuService.selectMenuList(menu, loginUser.getUser().getUserId());
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", menuService.selectBrandMenuListByRoleId(brandId));
        ajax.put("menus", menuService.buildMenuTreeSelectVo(menus));
        return ajax;
    }

    /**
     * 加载菜单列表树(授权所有品牌目录使用)
     *
     * @return
     */
    @ApiOperation("加载菜单列表树")
    @PreAuthorize("@ss.hasPermi('module:brandInfo:query')")
    @GetMapping(value = "/roleMenuTreeSelect")
    public Map roleMenuTreeSelect() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        PlatformMenu menu = new PlatformMenu();
        menu.setIsAuthorization(YesNoEnum.YES.getKey());
        List<PlatformMenu> menus = menuService.selectMenuList(menu, loginUser.getUser().getUserId());
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("menus", menuService.buildMenuTreeSelectVo(menus));
        return ajax;
    }

    /**
     * 授权品牌目录
     *
     * @param sysMenuVo
     * @return
     */
    @ApiOperation("授权品牌目录")
    @RequestMapping(value = "/authorizationBrandMenu", method = RequestMethod.POST)
    public AjaxResult authorizationBrandMenu(@RequestBody SysMenuVo sysMenuVo) {
        if (StringUtils.isEmpty(sysMenuVo.getBrandId()) || StringUtils.isEmpty(sysMenuVo.getCodeList()) || sysMenuVo.getCodeList().size() <= 0) {
            return AjaxResult.parameterError();
        }
        return toAjax(menuService.authorizationBrandMenu(sysMenuVo) ? 1 : 0);
    }

    /**
     * 授权所有品牌目录
     *
     * @param sysMenuVo
     * @return
     */
    @ApiOperation("授权所有品牌目录")
    @RequestMapping(value = "/authorizationAllBrandMenu", method = RequestMethod.POST)
    public AjaxResult authorizationAllBrandMenu(@RequestBody SysMenuVo sysMenuVo) {
        if (StringUtils.isEmpty(sysMenuVo.getCodeList()) || sysMenuVo.getCodeList().size() <= 0) {
            return AjaxResult.parameterError();
        }
        return toAjax(menuService.authorizationAllBrandMenu(sysMenuVo) ? 1 : 0);
    }

    /**
     * 设置品牌用户登录状态
     *
     * @param sysUser
     * @return
     */
    @ApiOperation("设置品牌用户登录状态")
    @RequestMapping(value = "/updateBrandLoginStatus", method = RequestMethod.POST)
    public AjaxResult updateBrandLoginStatus(@RequestBody SysUser sysUser) {
        if (StringUtil.isEmpty(sysUser.getBrandId()) || StringUtil.isEmpty(sysUser.getStatus())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iBrandInfoService.updateBrandLoginStatus(sysUser) ? 1 : 0);
    }

    /**
     * 设置门店用户登录状态
     *
     * @param sysUser
     * @return
     */
    @ApiOperation("设置门店用户登录状态")
    @RequestMapping(value = "/updateStoreLoginStatus", method = RequestMethod.POST)
    public AjaxResult updateStoreLoginStatus(@RequestBody SysUser sysUser) {
        if (StringUtil.isEmpty(sysUser.getStoreId()) || StringUtil.isEmpty(sysUser.getStatus())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iBrandInfoService.updateStoreLoginStatus(sysUser) ? 1 : 0);
    }



}
